﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using hou_elearning.Business;
using hou_elearning.Entity;

public partial class Controls_ucCalendar : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            napDdlKhoahoc();
        }
    }
    protected void ddlKhoahocxem_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["PK_iKhoahocID_Lich"] = ddlKhoahocxem.SelectedValue;
        ddlKhoahocxem.Items.FindByValue(ddlKhoahocxem.SelectedValue);
    }

    /// <summary>
    /// Nạp DropDownList khóa học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public void napDdlKhoahoc()
    {
        List<KhoahocEntity> lstKhoahoc = KhoahocBRL.GetAll();
        ddlKhoahocxem.DataSource = lstKhoahoc;
        ddlKhoahocxem.DataTextField = "sTenkhoahoc";
        ddlKhoahocxem.DataValueField = "PK_iKhoahocID";
        ddlKhoahocxem.DataBind();
        lstKhoahoc = null;
    }

    /// <summary>
    /// Lấy ID của khóa học đang được chọn
    /// </summary>
    /// <author>liemqv</author>
    /// <create>07/06/2012</create>
    /// 
    public Int64 GetPK_iKhoahocID()
    {
        Int64 iRes = 0;
        if (Session["PK_iKhoahocID_Lich"] != null)
        {
            iRes = Int64.Parse(Session["PK_iKhoahocID_Lich"].ToString());
        }
        else
        {
            iRes = Int64.Parse(ddlKhoahocxem.SelectedValue);
            Session["PK_iKhoahocID_Lich"] = iRes;
        }
        return iRes;
    }

    /// <summary>
    /// Nạp JavaScript FullCalendar lịch học
    /// </summary>
    /// <author>liemqv</author>
    /// <create>08/06/2012</create>
    ///
    public String sCalendarEnvent()
    {
        List<LichhocEntity> lstLichhoc = LichhocBRL.GetByFK_iKhoahocID(GetPK_iKhoahocID());
        String sRes = "";
        String sTmp = "";
        Khoahoc_BaihocEntity oKhoahocBaihoc = null;
        BaihocEntity oBaihoc = null;
        String sTitle = "";
        String sGiohoc = "";
        short iYear = 0;
        short iMonth = 0;
        short iDay = 0;
        if (lstLichhoc.Count > 0)
        {
            for (int i = 0; i < lstLichhoc.Count - 1; ++i)
            {
                oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(lstLichhoc[i].FK_iKhoahoc_BaihocID);
                oBaihoc = BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID);
                sTitle = oBaihoc.sTenbaihoc; // +" " + lstLichhoc[i].dGiobatdau.ToString("HH:mm") + "-" + lstLichhoc[i].dGioketthuc.ToString("HH:mm");
                iYear = short.Parse(lstLichhoc[i].dNgayhoc.ToString("yyyy"));
                iMonth = short.Parse(lstLichhoc[i].dNgayhoc.ToString("MM"));
                iMonth -= 1;
                sGiohoc = " (giờ học " + lstLichhoc[i].dGiobatdau.ToString("HH:mm") + " -> " + lstLichhoc[i].dGioketthuc.ToString("HH:mm") + ", ngày " + lstLichhoc[i].dNgayhoc.ToString("dd/MM/yyyy") + ")";
                iDay = short.Parse(lstLichhoc[i].dNgayhoc.ToString("dd"));
                sTmp = String.Format(@"title: '{0}',start: new Date({1}, {2}, {3})", sTitle + sGiohoc, iYear, iMonth, iDay);
                sTmp = "{" + sTmp + "},";
                sRes += sTmp;
                sTmp = "";
            }
            oKhoahocBaihoc = Khoahoc_BaihocBRL.GetOne(lstLichhoc[lstLichhoc.Count - 1].FK_iKhoahoc_BaihocID);
            oBaihoc = BaihocBRL.GetOne(oKhoahocBaihoc.FK_iBaihocID);
            sTitle = oBaihoc.sTenbaihoc; // +" " + lstLichhoc[lstLichhoc.Count - 1].dGiobatdau.ToString("HH:mm") + "-" + lstLichhoc[lstLichhoc.Count - 1].dGioketthuc.ToString("HH:mm");
            iYear = short.Parse(lstLichhoc[lstLichhoc.Count - 1].dNgayhoc.ToString("yyyy"));
            iMonth = short.Parse(lstLichhoc[lstLichhoc.Count - 1].dNgayhoc.ToString("MM"));
            iMonth -= 1;
            sGiohoc = " (giờ học " + lstLichhoc[lstLichhoc.Count - 1].dGiobatdau.ToString("HH:mm") + " -> " + lstLichhoc[lstLichhoc.Count - 1].dGioketthuc.ToString("HH:mm") + ", ngày " + lstLichhoc[lstLichhoc.Count - 1].dNgayhoc.ToString("dd/MM/yyyy") + ")";
            iDay = short.Parse(lstLichhoc[lstLichhoc.Count - 1].dNgayhoc.ToString("dd"));
            sTmp += String.Format(@"title: '{0}',start: new Date({1}, {2}, {3})", sTitle + sGiohoc, iYear, iMonth, iDay);
            sTmp = "{" + sTmp + "}";
            sRes += sTmp;
        }
        return sRes;
    }
}